<template>
  <a-modal
      :title="title"
      :width="800"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
      wrapClassName="ant-modal-cust-warp"
      style="top:5%;height: 85%;overflow-y: hidden">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" v-bind="layout" :model="model" :rules="rules">
        <a-form-model-item label="字段" required prop="field">
          <a-input v-model="model.field" :disabled="formDisabled" placeholder="请输入字段"/>
        </a-form-model-item>
        <a-form-model-item label="说明" required prop="illustrate">
          <a-input v-model="model.illustrate" :disabled="formDisabled" placeholder="请输入说明"/>
        </a-form-model-item>
        <a-form-model-item label="名称" required prop="name">
          <a-input v-model="model.name" :disabled="formDisabled" placeholder="请输入名称"/>
        </a-form-model-item>
        <a-form-model-item label="取值" required prop="value">
          <a-input v-model="model.value" :disabled="formDisabled" placeholder="请输入取值"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import {save} from '@/services/dict'

export default {
  name: "DictForm",
  data() {
    return {
      title: "操作",
      visible: false,
      formDisabled: false,
      isOkDisabled: false,
      model: {},
      layout: {
        labelCol: {span: 3},
        wrapperCol: {span: 20},
      },
      confirmLoading: false,
      rules: {
        field: [
          {required: true, message: '请输入字段!'},
        ],
        name: [
          {required: true, message: '请输入名称!'},
        ],
        illustrate: [
          {required: true, message: '请输入说明!'},
        ],
        value: [
          {required: true, message: '请输入取值!'},
        ]
      },
    }
  },
  methods: {
    add() {
      this.title = "添加字典";
      this.model = {};
      this.formDisabled = false
      this.isOkDisabled = false
      this.visible = true;
    },
    edit(record) {
      this.title = "编辑字典";
      this.model = Object.assign({}, record);
      this.formDisabled = false
      this.isOkDisabled = false
      this.visible = true;
    },
    view(record) {
      this.title = "查看字典";
      this.model = Object.assign({}, record);
      this.formDisabled = true
      this.isOkDisabled = true
      this.visible = true;
    },
    submit() {
      if (this.isOkDisabled) {
        this.visible = false;
      } else {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.confirmLoading = true;
            save(this.model).then(res => {
              if (res.data.code === 200) {
                this.$message.success('操作成功' || res.data.msg);
                this.$emit('refresh');
              } else {
                this.$message.error('操作失败' || res.data.msg);
              }
            }).catch(error => {
              this.$message.error('操作失败' || error.message);
            }).finally(() => {
              this.confirmLoading = false;
            })
            this.visible = false;
          }
        })
      }
    },
    close() {
      this.$refs.form.clearValidate();
      this.$emit('close');
      this.visible = false;
      this.model = {}
    },
    handleOk() {
      this.submit()
    },
    handleCancel() {
      this.close()
    },
  }
}
</script>

<style scoped>

</style>
